package View
{
	import Mode.RoleType;
	
	import laya.display.Animation;
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.maths.Rectangle;
	
	/**
	 * 类描述
	 * @author sixsir
	 * @date 2016-11-20下午4:46:13
	 */
	public class Role extends Sprite
	{
		public function Role()
		{
			super();
			
			body = new Animation();
		}
		
		public function init(type: String, camp: Number, hp: Number, speed: Number, hitRadius: Number):void
		{
			this.visible = true;
			this.type = type;
			this.camp = camp;
			this.hp = hp;
			this.speed = speed;
			this.hitRadius = hitRadius;
			
			this.addChild(body);
			body.on(Event.COMPLETE, this, onPlayComplete);
			
			playAction("fly");
		}
		
		public function playAction(action: String): void 
		{
			this.action = action;
			//根据类型播放动画
			this.body.play(0, true, type+"_"+action);
			//获取动画大小区域
			var bound: Rectangle = this.body.getBounds();
			//设置机身剧中
			this.body.pos(-bound.width / 2, -bound.height / 2);
		}
		
		private function onPlayComplete(): void 
		{
			//如果是击毁动画，则隐藏对象
			if (this.action == "down") 
			{
				//停止动画播放
				this.body.stop();
				//隐藏显示，通过此标记，在下帧进行回收
				this.visible = false;
			} 
			else if(this.action == "hit") 
			{
				//如果是被击动画播放完毕，则接着播放飞行动画
				this.playAction("fly");
			}
		}
		
		public function get isBullet():Boolean
		{
			return type == RoleType.Bullet1;
		}
		
		public function dispose():void
		{
			body.off(Event.COMPLETE, this, onPlayComplete);
			body.clear();
			body.removeSelf();
		}
		
		private var body:Animation;
		
		//定义碰撞体的类型
		public var type: String;
		//阵营,0：我方，1：敌方
		public var camp: Number;
		//血量
		public var hp: Number;
		//飞行速度
		public var speed: Number;
		//攻击半径
		public var hitRadius: Number;
		//射击类型
		public var shootType: Number = 0;
		//射击间隔
		public var shootInterval: Number = 500;
		//下次射击时间
		public var shootTime: Number;
		//当前动作
		public var action: String;
		
	}
}